package com.neusoft.nemp.controller;

import com.neusoft.nemp.entity.User;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;

public class TableViewController {
    @FXML
    private TableView<User> tableView;
    @FXML
    private TableColumn<User,String> colId;
    @FXML
    private TableColumn<User,String> colName;
    @FXML
    private TableColumn<User,Void> colDetail;
    @FXML
    private TableColumn<User,Void> colAssign;

    private ObservableList<User> tableList = FXCollections.observableArrayList();

    @FXML
    private DatePicker datePicker;

    @FXML
    public void initialize(){
        colId.setCellValueFactory(new PropertyValueFactory<>("id"));
        colName.setCellValueFactory(new PropertyValueFactory<>("name"));
        tableList.add(new User("1","zhangshan"));
        tableList.add(new User("2","lishi"));
        tableList.add(new User("3","wangwu"));

        tableView.setItems(tableList);
        //动态添加按钮列
        colDetail.setCellFactory(cols->new TableCell<>(){
            private Button btn = new Button("详情");
            {
                btn.setOnAction(e->{
                    User user = getTableView().getItems().get(getIndex());
                    System.out.println("点击了详情：" + user.getId() + "可以弹出窗口进行其他操作");
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(empty?null:btn);
            }
        });
        //动态添加按钮列
        colAssign.setCellFactory(cols->new TableCell<>(){
            private Button btn = new Button("指派");
            {
                btn.setOnAction(e->{
                    User user = getTableView().getItems().get(getIndex());
                    System.out.println("点击了指派：" + user.getId() + "可以弹出窗口进行其他操作");
                    Stage stage = new Stage();
                    FXMLLoader fxmlLoader = new FXMLLoader();
                    URL url = getClass().getResource("../view/LoginView.fxml");
                    fxmlLoader.setLocation(url);
                    Parent root = null;
                    try {
                        root = fxmlLoader.load();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    Scene scene = new Scene(root);
                    stage.setScene(scene);
                    stage.showAndWait();
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(empty?null:btn);
            }
        });
        //日期控件使用
        //设置默认值为当前系统时间
        datePicker.setValue(LocalDate.now());
        //添加监听时间，处理选择的时间
        datePicker.valueProperty().addListener(new ChangeListener<LocalDate>() {
            @Override
            public void changed(ObservableValue<? extends LocalDate> observableValue, LocalDate oldDate, LocalDate newDate) {
                System.out.println("选择的日期为：" + newDate);
            }
        });
    }
}
